<?php
//
//	file: includes/aucp/handler_pm_rules.php
//	begin: 2006-07-10
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: this is the main class for pm filters

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

class aucp_pm_rules
{
	// string vars
	var $data_flag, $data_time, $from_cache;
	// array vars
	var $data;

	// @constructor
	function __constructor()
	{
		$this->data = array();
	}

	// backward compatibility
	function aucp_pm_rules()
	{
		return $this->__constructor();
	}

	// @public get rules from cache/db
	function read($force=false)
	{
		global $config;

		// already read
		if ( $this->data_flag && !$force )
		{
			return;
		}

		// read data
		$config->data['cache_disabled_pm_boxes'] |= empty($config->data['cache_key']);
		$db_cached = new cache('dta_pm_boxes', $config->data['cache_path'], $config->data['cache_disabled_pm_boxes']);
		$sql = 'SELECT *
				FROM ' . PM_BOXES_TABLE . '
				ORDER BY pm_box_group';
		$this->data = $db_cached->sql_query($sql, __LINE__, __FILE__, $force, 'pm_box_id');
		$this->data_flag = true;
		$this->data_time = $db_cached->data_time;
		$this->from_cache = $db_cached->from_cache;
	}

	// @public runs through rules and returns all rules for that user
	function get_rules($user_id)
	{
		global $view_user, $user;
		if($user->data['user_id'] == $user_id)
		{
			$group_ids = $user->get_groups_list();
		}
		elseif($view_user->data['user_id'] == $user_id)
		{
			$group_ids = $view_user->get_groups_list();
		}
		else
		{
			$temp_user = new user();
			$temp_user->read($user_id);
			$group_ids = $temp_user->get_groups_list();
			unset($temp_user);
		}
		$boxes = array();
		foreach ($this->data as $row)
		{
			if (in_array($row['pm_box_group'], $group_ids))
			{
				$boxes[$row['pm_box_id']] = $row;
			}
		}
		return $boxes;
	}


	// @public create new pm filter
	function create_rule($name, $img, $group='', $limit='', $type=PM_BOXES_SAVE)
	{
		global $user, $config, $db;

		$group = ($group == '') ? $user->data['group_id'] : $group;

		// check auth
		if ( (!$config->data['pm_box_create'] && !in_array(GROUP_FOUNDER, $user->get_groups_list()) ) || !$user->auth(POST_GROUPS_URL, 'ucp_pm_create', $group) )
		{
			message_die(GENERAL_ERROR, 'Not_authorized');
		}

		// check limit
		$sql = 'SELECT group_pm_boxes
				FROM ' . GROUPS_TABLE . '
				WHERE group_id = "' . $group . '"';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);
		$group_boxes_count = intval($row['group_pm_boxes']);
		if ($group_boxes_count >= $config->data['pm_box_max'])
		{
			message_die(GENERAL_MESSAGE, 'Pm_boxes_limit');
		}

		// do the query
		$limit = ($limit == '') ? $config->data['pm_box_max'] : $limit;
		$sql = 'INSERT INTO ' . PM_BOXES_TABLE . '
			(pm_box_group, pm_box_txt, pm_box_img, pm_box_limit, pm_box_type)
			VALUES ("' . $group . '", "' . $name . '", "' . $img . '", "' . $limit . "', '" . $type . '")';

		if ($db->sql_query($sql, false, __LINE__, __FILE__))
		{
			// update count
			$group_boxes_count++;
			$sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_pm_boxes = "' . $group_boxes_count . '"
				WHERE group_id = "' . $group . '"';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}

	// @public edit existing pm filter
	function edit_rule($id, $name, $img, $group='', $limit='', $type=PM_BOXES_SAVE)
	{
		global $db, $user;
		$group = ($group == '') ? $user->data['group_id'] : $group;

		$sql = 'UPDATE ' . PM_BOXES_TABLE . '
			SET pm_box_txt = "' . $name . '",
				pm_box_img = "' . $img . '",
				pm_box_limit = "' . $limit . '",
				pm_box_type = "' . $type . '"
			WHERE pm_box_id = "' . $id . '"
				AND pm_box_group = "' . $group . '"';
		return $db->sql_query($sql, false, __LINE__, __FILE__);
	}

	// @public delete existing pm filter
	function delete_rule($id, $group='')
	{
		global $user, $config, $db;

		$group = ($group == '') ? $user->data['group_id'] : $group;

		// check auth
		if ( (!$config->data['pm_box_create'] && !in_array(GROUP_FOUNDER, $user->get_groups_list()) ) || !$user->auth(POST_GROUPS_URL, 'ucp_pm_create', $group) )
		{
			message_die(GENERAL_ERROR, 'Not_authorized');
		}

		// get count
		$sql = 'SELECT group_pm_boxes
				FROM ' . GROUPS_TABLE . '
				WHERE group_id = "' . $group . '"';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$row = $db->sql_fetchrow($result);
		$group_boxes = intval($row['group_pm_boxes']);

		// do the query
		$sql = 'DELETE FROM' . PM_BOXES_TABLE . '
			WHERE pm_box_id = "' . $id . '"';

		if ($db->sql_query($sql, false, __LINE__, __FILE__))
		{
			// update count
			unset($this->data[$id]);
			$group_boxes--;
			$sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_pm_boxes = "' . $group_boxes . '"
				WHERE group_id = "' . $group . '"';
			$db->sql_query($sql, false, __LINE__, __FILE__);

			// get orhpaned pms
			$sql = 'SELECT privmsgs_id FROM' . PRIVMSGS_TABLE . '
				WHERE privmsgs_box = "' . $box . '"';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$del_ids[] = intval($row['privmsgs_id']);
			}
			$db->sql_freeresult($result);
			$del_ids = implode(', ', $del_ids);

			// delete them
			$sql = 'DELETE FROM' . PRIVMSGS_TABLE . '
				WHERE privmsgs_id IN ("' . $del_ids . '")';
			$db->sql_query($sql, false, __LINE__, __FILE__);
			unset($del_ids);
		}
	}

	// @private remove pms in non-existant boxes
	function prune_rule($rule)
	{
		global $db;


	}

	// @public sync and prune boxes and filters.
	function resync()
	{
		global $db;

		// force current boxes
		$this->validate();
		$this->read(true);
		$current_boxes = implode(', ', array_keys($this->data));

		// get orphaned pms
		$sql = 'SELECT * FROM' . PRIVMSGS_TABLE . '
			WHERE privmsgs_box NOT IN ("' . $current_boxes . '")';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$info = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$del_ids[] = intval($row['privmsgs_id']);
			$del_text_ids[] = $row['privmsgs_textid'];
		}
		$db->sql_freeresult($result);
		unset($current_boxes);

		// delete orphaned pms
		$del_ids = implode(', ', $del_ids);
		$sql = 'DELETE FROM' . PRIVMSGS_TABLE . '
			WHERE privmsgs_id IN ("' . $del_ids . '")';
		$db->sql_query($sql, false, __LINE__, __FILE__);
		unset($del_ids);

		// delete orphaned pm texts
		$sql = 'SELECT * FROM' . PRIVMSGS_TABLE . '
			WHERE privmsgs_textid IN ("' . implode(', ', $del_text_ids) . '")';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		// we still have an active pm, so let's not delete the text for it!
		$keep_ids = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$keep_ids[] = intval($row['privmsgs_id']);
		}
		if (!empty($keep_ids))
		{
			$del_text_ids = array_diff($del_text_ids, $keep_ids);
		}
		$del_text_ids = implode(', ', $del_text_ids);

		$sql = 'DELETE FROM' . PRIVMSGS_TEXT_TABLE . '
			WHERE privmsgs_text_id IN ("' . $del_text_ids . '")';
		$db->sql_query($sql, false, __LINE__, __FILE__);
		unset($del_text_ids);
	}

	// @public displays all filters for a user
	function display()
	{

	}
}
?>